﻿using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Eneta.Portal.Controls
{
    /// <summary>
    /// Class for ENETA main menu control.
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class MainMenuControl : HierarchicalDataBoundControl
    {
        /// <summary>
        /// Renders begin tag of menu element.
        /// </summary>
        /// <param name="writer">HtmlTextWriter that accepts menu control output.</param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("<ul id=\"menu\">");
        }
        /// <summary>
        /// Renders the body of menu.
        /// </summary>
        /// <param name="output">HtmlTextWriter that accepts menu control output.</param>
        protected override void RenderContents(HtmlTextWriter output)
        {
            EnsureDataBound();

            var source = (SiteMapDataSource)GetDataSource();
            var provider = source.Provider;
            
            if (provider == null)
                return;
            if (provider.RootNode == null)
                return;

            var i = 0;

            foreach (SiteMapNode childNode in provider.RootNode.ChildNodes)
            {
                i++;
                output.Write("<li id=\"" + i + "\"");
                if (IsNodeActive(childNode, provider))
                    output.Write(" class='active'");
                output.Write("><a href=\"" + childNode.Url + "\">");
                output.Write(childNode.Title);
                //if (childNode.PreviousSibling == null && IsNodeActive(childNode,provider))
                if (childNode.PreviousSibling == null)
                    output.Write("<span></span>");
                output.Write("</a>");

                if (childNode.ChildNodes.Count > 0)
                {
                    output.Write("<ul class=\"submenu\">");
                    foreach (SiteMapNode childChildNode in childNode.ChildNodes)
                    {
                        output.Write("<li>");
                        output.Write("<a href=\"" + childChildNode.Url + "\">");
                        output.Write(childChildNode.Title);
                        output.Write("</a>");
                        output.Write("</li>");
                    }
                    output.Write("</ul>");
                }

                output.Write("</li>");
            }
        }

        private bool IsNodeActive(SiteMapNode node, SiteMapProvider provider)
        {
            if (node.Equals(provider.CurrentNode))
                return true;
            if (node.ChildNodes.Count != 0)
                if (node.ChildNodes.Contains(provider.CurrentNode))
                    return true;
            return false;
        }

        /// <summary>
        /// Renders the closing tag of menu.
        /// </summary>
        /// <param name="writer">HtmlTextWriter that accepts menu control output.</param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("</ul>");
        }
    }
}
